package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.Mmas8Info;
import org.dtrd.modules.evaluation.entity.dto.Mmas8DTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdMmas8;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdMmas8Mapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdMmas8Service;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.StringJoiner;

/**
 * <p>
 * Morisky用药依从性评估MMAS8 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdMmas8ServiceImpl extends ServiceImpl<DtrdEntEvaluationRdMmas8Mapper, DtrdEntEvaluationRdMmas8> implements IDtrdEntEvaluationRdMmas8Service {

    @Override
    public Mmas8Info saveMMAS8Info(Mmas8DTO dto) {
        StringJoiner joiner = new StringJoiner(",");
        float score = 0;
        int tmpAnswer;
        for (int i = 0; i < dto.getAnswers().length; i++) {
            tmpAnswer = dto.getAnswers()[i];
            joiner.add(tmpAnswer+"");
            if (i + 1 <= 7) {
                score += 1 - tmpAnswer;
            } else if (i + 1 == 8) {
                switch (tmpAnswer) {
                    case 1:
                        score += 1;
                        break;
                    case 2:
                        score += 0.75;
                        break;
                    case 3:
                        score += 0.50;
                        break;
                    case 4:
                        score += 0.25;
                        break;
                    case 5:
                        score += 0;
                        break;
                    default:
                        break;
                }
            }
        }
        String output = "您的总分为"+score+"分，用药依从性";
        if (score < 6) {
            output += "不理想。";
        } else if (score >= 6 && score < 10) {
            output += "适中。";
        } else if (score == 10) {
            output += "较好。";
        }
        // 保存到数据库
        DtrdEntEvaluationRdMmas8 mmas8 = new DtrdEntEvaluationRdMmas8();
        mmas8.setCreateTime(new Date());
        mmas8.setOutput(output);
        mmas8.setScore(score);
        mmas8.setInput(joiner.toString());
        mmas8.setUserType(dto.getUserType());
        if (dto.getPatientId() != null) {
            mmas8.setPatientId(dto.getPatientId());
            save(mmas8);
        }
        return new Mmas8Info().parseFromPo(mmas8);
    }

    @Override
    public Mmas8Info getMMAS8InfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdMmas8> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(DtrdEntEvaluationRdMmas8::getPatientId, patientId)
                .eq(DtrdEntEvaluationRdMmas8::getIsDel, 0)
                .orderByDesc(DtrdEntEvaluationRdMmas8::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdMmas8 mmas8 = baseMapper.selectOne(lambdaQueryWrapper);
        Mmas8Info mmas8Info = new Mmas8Info();
        if (mmas8 != null) {
            mmas8Info = mmas8Info.parseFromPo(mmas8);
        }
        return mmas8Info;
    }
}
